<?php
/**
 * 拍照肤质检测数据的model
 *
 * Author: Zeon
 * Date: 2018/4/4
 * Created by Panxsoft.
 */
namespace App\Models\Store;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * Class CamSkinRecord
 *
 * @property float health
 * @property float dark_circle
 * @property float acne
 * @property float stain
 * @property Carbon created_at
 * @property Carbon updated_at
 * @package App\Models\Store
 */
class CamSkinRecord extends Model
{
    protected $table = 'cam_skin_records';
    
    protected static $unguarded = true;

    public function skinRecord(): BelongsTo
    {
        return $this->belongsTo(SkinRecord::class);
    }

    /**
     * @param array $skin_record_camera
     * @return mixed
     */
    public static function checkHasSkinRecord(array $skin_record_camera)
    {
        $cam_skin_record = self::where('skin_record_id', $skin_record_camera['skin_record_id'])
            ->first();

        return $cam_skin_record;
    }

    /**
     * 返回健康度击败全国的百分数
     *
     * @return string
     */
    public function getHealthBeatCountryPercent(): string
    {
        $health = $this->health;

        $all_counts = self::count();
        if (! $all_counts) return '0%';

        $below_counts = self::where('health', '<', $health)->count();

        $percent = ($below_counts / $all_counts) * 100;

        // 56.243 => 56
        $format_percent = (int) round($percent);

        return $format_percent . '%';
    }
}
